package com.caiyi.financial.nirvana.sms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import static com.caiyi.financial.nirvana.sms.util.HttpClientUtil.doPost;

/**
 * @Description:
 * @author:zhaocc
 * @date 2018/10/915:33
 */
public class AlarmUtil {
    private static final Logger logger = LoggerFactory.getLogger(AlarmUtil.class);
    private static final String ACCOUNT = "youyu88hy";
    private static final String PASSWORD = "EQrW8u";
    private static final String CONTENT = "【彩亿科技】验证码短信严重告警，3分钟之内没有短信发送记录，请注意查看！！！";
    private static final String URL = "http://www.api.zthysms.com/sendSmsBatch.do";

    public static void sendAlarmSms() {
        try {
            Map params = buildParam();
            String response = doPost(URL, params);
            if (response == null) {
                response = doPost(URL, params);
            }
            logger.info("ZhuTongGateway send url : {}, params : {}, result : {}", URL, params, response);
        } catch (Exception e) {
            logger.error("sendAlarm error ! ", e);
        }
    }

    private static Map buildParam() {
        Map<String, String> map = new HashMap<>();
        map.put("username", ACCOUNT);
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String tkey = format.format(LocalDateTime.now());
        map.put("tkey", tkey);
        String password = MD5Util.encode(MD5Util.encode(PASSWORD).toLowerCase() + tkey).toLowerCase();
        map.put("password", password);
        map.put("mobile", Constant.PHONE);
        map.put("content", CONTENT);
        return map;
    }
}
